package dao.impl;

import bean.Role;
import bean.Users;
import dao.UsersDao;
import util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UsersDaoImpl extends DBUtil implements UsersDao {

    /**
     * 用户名和密码的获取
     * @param userName
     * @param passWord
     * @return
     */
    @Override
    public Users login(String userName, String passWord) {
        Users users = null;

        try {
            String sql = " select * from users where loginname=? and password=? ";
            ArrayList arrayList = new ArrayList();
            arrayList.add(userName);
            arrayList.add(passWord);

            //通过用户名和密码查询，获取结果
            resultSet = query(sql,arrayList);

            //查询无结果，返回空
            if (resultSet==null){
                return null;
            }

            //存在结果
            while (resultSet.next()){
                users = new Users();
                users.setLoginName(userName);
                users.setRealName(resultSet.getString("realname"));
                users.setUserId(resultSet.getInt("userid"));
                users.setRoleId(resultSet.getInt("roleid"));

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }


        return users;
    }


    /**
     * 获取用户的信息列表
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public List<Users> getUsersList(int pageIndex, Integer pageSize) {

        List<Users> usersList = new ArrayList<>();


        try {
            String sql = " select userid,loginname,realname,rolename from users u,role r where u.roleid=r.roleid limit ?,? ";
            List params = new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);

            resultSet = query(sql,params);

            while (resultSet.next()){

                //取出用户表数据
                Users users = new Users();
                users.setUserId(resultSet.getInt("userid"));
                users.setLoginName(resultSet.getString("loginname"));
                users.setRealName(resultSet.getString("realname"));

                //取出角色表数据
                Role role = new Role();
                role.setRoleName(resultSet.getString("rolename"));

                //将角色名存入用户数据中
                users.setRole(role);

                //将得到用户数据加入集合
                usersList.add(users);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }


        return usersList;
    }


    /**
     * 获取查询信息总条数
     * @return
     */
    @Override
    public int total() {

        int total = 0;

        try {
            String sql = " select count(1) from users u,role r where u.roleid=r.roleid ";

            resultSet = query(sql);

            while (resultSet.next()){
                total = resultSet.getInt(1);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }


        return total;
    }


    /**
     * 新增用户信息
     * @return
     */
    @Override
    public int insert(Users users) {
        int i = 0;

        try {
            String sql = " insert into users values(null,?,?,?,?,?,?,?,?,?,?) ";
            List param = new ArrayList();

            param.add(users.getLoginName());
            param.add(users.getPassWord());
            param.add(users.getRealName());
            param.add(users.getSex());
            param.add(users.getEmail());
            param.add(users.getAddress());
            param.add(users.getPhone());
            param.add(users.getCardId());
            param.add(users.getDesc());
            param.add(users.getRoleId());

            i = update(sql,param);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }


        return i;
    }


    /**
     * 通过用户主键获取用户信息
     * @param userId
     * @return
     */
    @Override
    public Users findById(int userId) {

        Users user = new Users();

        try {
            String sql = " select * from users where userid=? ";

            List params = new ArrayList();
            params.add(userId);
            resultSet = query(sql,params);

            while (resultSet.next()){
                user.setUserId(resultSet.getInt("userid"));
                user.setLoginName(resultSet.getString("loginname"));
                user.setPassWord(resultSet.getString("password"));
                user.setRealName(resultSet.getString("realname"));
                user.setSex(resultSet.getInt("sex"));
                user.setEmail(resultSet.getString("email"));
                user.setAddress(resultSet.getString("address"));
                user.setPhone(resultSet.getString("phone"));
                user.setCardId(resultSet.getString("cardid"));
                user.setDesc(resultSet.getString("desc"));
                user.setRoleId(resultSet.getInt("roleid"));

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }


        return user;
    }


    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public int update(Users user) {

        int updateRows = 0;

        try {
            String sql = " update users set loginname=?,password=?,realname=?,sex=?,email=?,address=?,phone=?,cardid=?,`desc`=?,roleid=? where userid=? ";

            List params = new ArrayList();
            params.add(user.getLoginName());
            params.add(user.getPassWord());
            params.add(user.getRealName());
            params.add(user.getSex());
            params.add(user.getEmail());
            params.add(user.getAddress());
            params.add(user.getPhone());
            params.add(user.getCardId());
            params.add(user.getDesc());
            params.add(user.getRoleId());
            params.add(user.getUserId());

            updateRows = update(sql,params);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }


        return updateRows;
    }


    /**
     * 删除用户信息
     * @param userId
     * @return
     */
    @Override
    public int delete(int userId) {
        int rows = 0;

        try {
            String sql = " delete from users where userid=? ";
            List params = new ArrayList();
            params.add(userId);

            rows = update(sql,params);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return rows;
    }
}
